package main

import "fmt"

func main() {
	//使用for-range遍历map
	cities := make(map[string]string)
	cities["no1"] = "北京"
	cities["no2"] = "天津"
	cities["no3"] = "上海"

	for k, v := range cities {
		fmt.Printf("k=%v v=%v \n", k, v)
	}

	//获取map中key-value的个数
	fmt.Println("cities 有", len(cities), "对key-value")

	//使用for-range遍历一个复杂结构的map
	studentsMap := make(map[string]map[string]string)
	studentsMap["stu01"] = make(map[string]string, 3) //这句话不能少!! 因为map中的map,同样需要先make才能使用,不然会报异常
	studentsMap["stu01"]["name"] = "tom"
	studentsMap["stu01"]["sex"] = "男"
	studentsMap["stu01"]["address"] = "北京长安街"

	studentsMap["stu02"] = make(map[string]string, 3) //这句话不能少!!
	studentsMap["stu02"]["name"] = "mary"
	studentsMap["stu02"]["sex"] = "女"
	studentsMap["stu02"]["address"] = "上海黄浦江"

	fmt.Println(studentsMap)
	fmt.Println(studentsMap["stu02"])
	fmt.Println(studentsMap["stu02"]["address"])

	for k1, v1 := range studentsMap {
		fmt.Println("k1=", k1)
		for k2, v2 := range v1 {
			fmt.Printf("\t k2=%v v2=%v\n", k2, v2)
		}
		fmt.Println()
	}

}
